

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CeleThunk 2.0.2
// Copyright (C) Yonsm 2009, All Rights Reserved.
#include <TSP.h>
#include <TAPI.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CeleTAPI helper
class CeleTAPI
{
public:
	HLINEAPP m_hLineApp;
	DWORD m_dwDevCount;
	DWORD m_dwAPIVer;

public:
	CeleTAPI()
	{
		m_dwAPIVer = 0x00020000;
		LINEINITIALIZEEXPARAMS liep;
		liep.dwTotalSize = sizeof(liep);
		liep.dwOptions = LINEINITIALIZEEXOPTION_USEEVENT;
		lineInitializeEx(&m_hLineApp, 0, 0, NULL, &m_dwDevCount, &m_dwAPIVer, &liep);
	}

	~CeleTAPI()
	{
		lineShutdown(m_hLineApp);
	}

public:
	DWORD GetDeviceID(PCTSTR ptzLineName = CELLTSP_LINENAME_STRING)
	{
		DWORD dwDevID = -1;

		LINEDEVCAPS* pCaps = (LINEDEVCAPS*) malloc(1024);
		pCaps->dwTotalSize = 1024;
		for (DWORD i = 0; i < m_dwDevCount; i++)
		{
			LINEEXTENSIONID LineExtensionID;
			if (lineNegotiateAPIVersion(m_hLineApp, i, 0x00020000, 0x00020000, &m_dwAPIVer, &LineExtensionID) == 0)
			{
				LONG lResult = lineGetDevCaps(m_hLineApp, i, m_dwAPIVer, 0, pCaps);
				if (pCaps->dwTotalSize < pCaps->dwNeededSize)
				{
					pCaps = (LINEDEVCAPS*) realloc(pCaps, pCaps->dwNeededSize);
					pCaps->dwTotalSize = pCaps->dwNeededSize;
					lResult = lineGetDevCaps(m_hLineApp, i, m_dwAPIVer, 0, pCaps);
				}

				if ((lResult == 0) && (_tcscmp((PTSTR) ((PBYTE) pCaps + pCaps->dwLineNameOffset), ptzLineName) == 0))
				{
					dwDevID = i;
				}
			}
		}

		free(pCaps);
		return dwDevID;
	}

	BOOL GetAddress(DWORD dwDevID, PTSTR ptzAdress)
	{
		LINEADDRESSCAPS* pCaps = (LINEADDRESSCAPS*) malloc(1024);
		pCaps->dwTotalSize = 1024;

		const DWORD dwAddressID = 0;
		LONG lResult = lineGetAddressCaps(m_hLineApp, dwDevID, dwAddressID, m_dwAPIVer, 0, pCaps);
		if (pCaps->dwTotalSize < pCaps->dwNeededSize)
		{
			pCaps = (LINEADDRESSCAPS*) realloc(pCaps, pCaps->dwNeededSize);
			pCaps->dwTotalSize = pCaps->dwNeededSize;
			lResult = lineGetAddressCaps(m_hLineApp, dwDevID, dwAddressID, m_dwAPIVer, 0, pCaps);
		}

		if ((lResult == 0) && pCaps->dwAddressSize)
		{
			memcpy(ptzAdress, (PTSTR) (((PBYTE) pCaps) + pCaps->dwAddressOffset), pCaps->dwAddressSize);
		}
		else
		{
			ptzAdress[0] = 0;
		}

		free(pCaps);
		return (BOOL) ptzAdress[0];
	}

	inline static BOOL GetPhoneNumber(PTSTR ptzNumber)
	{
		CeleTAPI tapi;
		return tapi.GetAddress(tapi.GetDeviceID(), ptzNumber);
	}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
